<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Navigation_library
{
	// Protected or private properties
	protected $_table;
	
	// Constructor
	public function __construct()
	{
		if (!isset($this->CI))
		{
			$this->CI =& get_instance();
		}
		
		$this->_table = $this->CI->config->item('database_tables');
	}

	// Public methods
	public function get_navigation($parent_id)
	{
		$this->CI->db->select('id, title, description, url, status, order, external');
		$this->CI->db->where('parent_id', $parent_id);
		$this->CI->db->where('status',1);
		$this->CI->db->order_by('order', 'ASC'); 
		
		$query = $this->CI->db->get($this->_table['site_menu']);
			
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}
	
	/*
	 *  For Shopper
	 */
	public function get_max_level($lang_id = 2)
	{
		$this->CI->db->select('MAX(LEVEL) AS LEVEL');
		$this->CI->db->where('status',1);
		$this->CI->db->where('lang_id',$lang_id);
		
		$query = $this->CI->db->get($this->_table['site__category']);
			
		if ($query->num_rows() > 0)
		{
			return $query->result_array();
		}
	}
	public function get_navi_items($lang_id = 2, $class ="", $id="")
	{
		$this->CI->db->select('*');
		$this->CI->db->where('status',1);
		$this->CI->db->where('lang_id',$lang_id);
		$this->CI->db->order_by('order', 'ASC'); 
		$tree = "";
		$query = $this->CI->db->get($this->_table['s_category']);
		
		if ($query->num_rows() > 0)
		{
			$return = $query->result_array();
			$parent = array();
			foreach($return as $item)
			{
				 if($item['level'] == 1)
				   $parent[] = $item;
			}
			
			$tree = "<ul class='" . $class . "' id='". $id."'>";
			foreach($parent as $item)
			{
				$tree = $tree . "<li><a href='javascript:void(0);' >" . $item['title'] ."</a>
									<ul>";
				foreach($return as $item1)
				{
					if ($item1['parent_id'] == $item['id'])
						//$tree = $tree . "<li><a href='/product/category/" . $item1['id'] . ".html' >". $item1['title'] . "</a></li>";
						$tree = $tree . "<li>" . p_category_url($item1['name'],$item1['id'])."</li>";					
				}
				
				$tree = $tree . "</ul>
								</li>";
			}
			$tree = $tree . "</ul>";
		
		}
		
		return $tree;
		
	}
}